<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>什么都论坛-后台帖子管理</title>
    <style>
        body {
            background: #f7f9fb;
            margin: 0;
            padding: 0;
        }

        .container {
            width: 90%;
            max-width: 1100px;
            margin: -50px auto;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 2px 12px rgba(80, 133, 203, 0.08);
            padding: 30px;
        }

        .container h2 {
            margin-top: 0;
            color: #0327b3;
            text-align: center;
            padding: 0;
            margin: 0 auto 10px;
        }

        .actions {
            margin-bottom: 20px;
            text-align: right;
        }

        .actions button {
            background: #2a4dd0;
            color: #fff;
            border: none;
            border-radius: 5px;
            padding: 8px 18px;
            font-size: 15px;
            cursor: pointer;
            transition: background 0.2s;
            margin-left: 10px;
        }

        .actions button:hover {
            background: #0327b3;
        }

        h3 {
            text-align: left;
            color: #2a4dd0;
            margin-top: 12px;
            margin-bottom: 10px;
            font-size: 20px;
            border-left: 4px solid #5085cb;
            padding-left: 10px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 30px;
            background: #f9fbfd;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 1px 4px rgba(80, 133, 203, 0.04);
        }

        th,
        td {
            padding: 12px 10px;
            text-align: left;
        }

        th {
            background: #5085cb;
            color: #fff;
            font-weight: 600;
            border-bottom: 2px solid #e3eaf6;
        }

        tr:nth-child(even) {
            background: #f4f6fa;
        }

        tr:hover {
            background: #eaf2ff;
            transition: background 0.2s;
        }

        td {
            color: #333;
            font-size: 15px;
            border-bottom: 1px solid #e3eaf6;
        }

        .content {
            width: 200px;
            max-width: 200px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            word-break: break-all;
        }

        .edit-btn {
            display: flex;
            gap: 8px;
            justify-content: flex-start;
            align-items: center;
            background: transparent;
            border: none;
            width: auto;
            padding: 0;
            margin: 10px;
        }

        .edit-btn a {
            display: inline-block;
            color: #fff;
            background: #5085cb;
            text-decoration: none;
            padding: 6px 16px;
            border-radius: 5px;
            font-size: 14px;
            margin: 0 2px;
            transition: background 0.2s, color 0.2s;
            box-shadow: 0 1px 3px rgba(80, 133, 203, 0.08);
        }

        .edit-btn a:hover {
            background: #0327b3;
            color: #fff;
        }

        .edit-btn a:last-child {
            background: #e74c3c;
        }

        .edit-btn a:last-child:hover {
            background: #c0392b;
            color: #fff;
        }


        .add-board {
            display: none;
            position: fixed;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.25);
            z-index: 9999;
            justify-content: center;
            align-items: center;
        }

        .add-board.active {
            display: flex;
        }

        .add-board form {
            background: #fff;
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(80, 133, 203, 0.18);
            padding: 36px 40px 28px 40px;
            min-width: 320px;
            display: flex;
            flex-direction: column;
            gap: 18px;
            align-items: stretch;
            position: relative;
        }

        .add-board label {
            font-weight: 600;
            color: #0327b3;
            margin-bottom: 6px;
        }

        .add-board input[type="text"] {
            padding: 10px 14px;
            border: 1.5px solid #d0d7e5;
            border-radius: 6px;
            font-size: 16px;
            outline: none;
            margin-bottom: 10px;
            transition: border 0.2s;
            background: #f7f9fb;
        }

        .add-board input[type="text"]:focus {
            border: 1.5px solid #5085cb;
            background: #f0f6ff;
        }

        .add-board input[type="submit"] {
            background: #2a4dd0;
            color: #fff;
            border: none;
            border-radius: 6px;
            padding: 10px 0;
            font-size: 17px;
            cursor: pointer;
            transition: background 0.2s;
            margin-top: 8px;
        }

        .add-board input[type="submit"]:hover {
            background: #0327b3;
        }

        .close-btn {
            position: absolute;
            top: 12px;
            right: 18px;
            background: transparent;
            border: none;
            color: #aaa;
            font-size: 22px;
            cursor: pointer;
            transition: color 0.2s;
        }

        .close-btn:hover {
            color: #e74c3c;
        }
    </style>
</head>

<body>
    <div class="container">
        <h2>帖子管理</h2>
        <div class="actions">
            <?php
            if (isset($_GET['board']) && $_GET['board'] == 1) {
                echo "<button onclick='location.href=\"../views/indexBms.php?action=post\"'>返回</button>";
                echo "<button onclick=\"document.querySelector('#boardName').value='';document.querySelector('.add-board').classList.add('active')\">版块添加</button>";
                if (isset($_GET['error']) && $_GET['error'] == '版块已存在') {
                    echo "<script>alert('该版块已存在，请重新输入')</script>";
                }
                if (isset($_GET['error']) && $_GET['error'] == '该版块下有帖子，请先删除帖子') {                    
                    echo "<script>alert('该版块下有帖子，请先删除帖子')</script>";
                }
            } else {
                echo "<button onclick='location.href=\"../views/indexBms.php?action=post&board=1\"'>版块管理</button>";
            }
            ?>
            <button onclick="location.reload()">刷新</button>
        </div>
        <!-- 弹窗表单部分 -->
        <div class="add-board">
            <form action="../control/doPost.php" method="post">
                <button type="button" class="close-btn"
                    onclick="document.querySelector('.add-board').classList.remove('active')">&times;</button>
                <label for="boardName">添加版块的名称</label>
                <input type="text" id="boardName" name="boardName" required>
                <input type="submit" value="添加">
            </form>
        </div>
        <?php
        require_once '../model/postBmsDao.php';
        $board = get_all_boards();
        $data = get_all_posts();
        if (isset($_GET['board']) && $_GET['board'] == 1) {
            echo "<table border='1' width='100%' align='center'>";
            echo "<tr><th width='300px'>版块id</th><th width='400px'>版块名称</th><th>操作</th></tr>";
            foreach ($board as $value) {
                echo "<tr>
                <td>" . $value['boardid'] . "</td>
                <td>" . $value['boardName'] . "</td>
                <td class='edit-btn' style='cursor: pointer;'>
                <a href='../control/doPost.php?action=delete_board&boardId=" . $value['boardid'] . "' onclick='return confirm(\"确认删除吗？\")'>删除</a>
                </td>
                </tr>";
            }
            echo "</table>";
            return;
        }

        foreach ($board as $b_value) {
            echo "<div><h3>" . $b_value['boardName'] . "版块</h3>";
            echo "<input type='hidden' id='boardId' value='" . $b_value['boardid'] . "'>";
            echo "<table border='1' width='100%' align='center'>";
            echo "<tr><th>帖子标题</th><th>帖子内容</th><th>发布时间</th><th>点赞量</th><th>发布人</th><th>操作</th></tr>";

            // 只显示属于当前版块的帖子
            foreach ($data as $value) {
                if ($value['boardId'] != $b_value['boardid'])
                    continue;

                $topicId = $value['topicId'];
                $boardName = $value['boardName'];
                $title = $value['title'];
                $content = $value['content'];
                $publishTime = $value['publishTime'];
                $userName = $value['uName'];
                $headImg = $value['head'];
                $likes = $value['likes'];
                $replyNumber = $value['replyNumber'];

                echo "<tr>
                <td class='content'>$title</td>";

                // 如果字符串包含<p>标签，则只显示第一个<p>标签之间的内容
                $showContent = $content;
                if (strpos($content, '<p>') !== false) {
                    if (preg_match('/<p>(.*?)<\/p>/is', $content, $matches)) {
                        $showContent = $matches[1];
                    }
                }
                echo "<td class='content'>$showContent</td>";

                echo "<td width='150px'>$publishTime</td>                
                <td>$likes</td>
                <td>$userName</td>
                <td class='edit-btn'>
                    <a href='../../bbs/views/PostDetails.php?topic_id=$topicId'>查看</a>";
                if ($value['good'] == 1) {
                    echo "<a href='../control/doPost.php?topicId=$topicId&isgood=0')>取消精华</a>";
                } else {
                    echo "<a href='../control/doPost.php?topicId=$topicId&isgood=1'>设为精华</a>";
                }
                ;
                echo "<a href='../control/doPost.php?action=delete_post&topic_id=$topicId' onclick='return confirm(\"确认删除吗？\")'>删除</a>                                        
                </td>
                </tr>";
            }
            echo "</table></div>";
        }
        ?>
    </div>

</body>


</html>